From 4b4a16157870d5816339700616f1efdb60fb59e3 Mon Sep 17 00:00:00 2001
From: phunkyfish <phunkyfish@gmail.com>
Date: Wed, 7 Oct 2020 18:12:10 +0100
Subject: [PATCH] Use thread sleep_for instead of p8 time utils

Signed-off-by: Bernd Kuhls <bernd.kuhls@t-online.de>
---
 src/lib/tsreader/DeMultiplexer.cpp   |  6 ++++--
 src/lib/tsreader/FileReader.cpp      |  5 +++--
 src/lib/tsreader/MemoryBuffer.cpp    |  5 +++--
 src/lib/tsreader/MepoRTSPClient.cpp  |  5 +++--
 src/lib/tsreader/MultiFileReader.cpp |  9 +++++----
 src/lib/tsreader/TSReader.cpp        |  5 +++--
 src/pvrclient-mediaportal.cpp        | 23 +++++++++++++----------
 7 files changed, 34 insertions(+), 24 deletions(-)

diff --git a/src/lib/tsreader/DeMultiplexer.cpp b/src/lib/tsreader/DeMultiplexer.cpp
index 63bf20e..436e452 100644
--- a/src/lib/tsreader/DeMultiplexer.cpp
+++ b/src/lib/tsreader/DeMultiplexer.cpp
@@ -37,6 +37,8 @@
 #include <kodi/General.h>  //for kodi::Log
 #include "TSReader.h"
 
+#include <thread>
+
 #define MAX_BUF_SIZE 8000
 #define BUFFER_LENGTH 0x1000
 #define READ_SIZE (1316*30)
@@ -83,7 +85,7 @@ namespace MPTV
         {
             size_t BytesRead = ReadFromFile();
             if (0 == BytesRead)
-                usleep(10000);
+                std::this_thread::sleep_for(std::chrono::milliseconds(10));
         }
         m_bStarting = false;
     }
@@ -248,7 +250,7 @@ namespace MPTV
         {
             size_t BytesRead = ReadFromFile();
             if (0 == BytesRead)
-                usleep(10000);
+                std::this_thread::sleep_for(std::chrono::milliseconds(10));
             dwBytesProcessed += BytesRead;
         }
 
diff --git a/src/lib/tsreader/FileReader.cpp b/src/lib/tsreader/FileReader.cpp
index 3df22f2..73b23af 100644
--- a/src/lib/tsreader/FileReader.cpp
+++ b/src/lib/tsreader/FileReader.cpp
@@ -37,10 +37,10 @@
 #include "TSDebug.h"
 #include "p8-platform/threads/threads.h"
 #include <algorithm> //std::min, std::max
-#include "p8-platform/util/timeutils.h" // for usleep
 #include "utils.h"
 #include <errno.h>
 
+#include <thread>
 
 /* indicate that caller can handle truncated reads, where function returns before entire buffer has been filled */
 #define READ_TRUNCATED 0x01
@@ -145,7 +145,8 @@ namespace MPTV
                     }
                 }
             }
-            usleep(20000);
+            std::this_thread::sleep_for(std::chrono::milliseconds(20));
+
         } while (--Tmo);
 
         if (Tmo)
diff --git a/src/lib/tsreader/MemoryBuffer.cpp b/src/lib/tsreader/MemoryBuffer.cpp
index 609b575..0e736f2 100644
--- a/src/lib/tsreader/MemoryBuffer.cpp
+++ b/src/lib/tsreader/MemoryBuffer.cpp
@@ -29,12 +29,13 @@
 
 #ifdef LIVE555
 
-#include "p8-platform/util/timeutils.h"
 #include "p8-platform/threads/mutex.h"
 #include "MemoryBuffer.h"
 #include <kodi/General.h> //for kodi::Log
 #include "TSDebug.h"
 
+#include <thread>
+
 #define MAX_MEMORY_BUFFER_SIZE (1024L*1024L*12L)
 
 CMemoryBuffer::CMemoryBuffer(void)
@@ -197,7 +198,7 @@ long CMemoryBuffer::PutBuffer(unsigned char *pbData, size_t lDataLength)
 
   if (sleep)
   {
-    usleep(10000);
+    std::this_thread::sleep_for(std::chrono::milliseconds(10));
   }
   return S_OK;
 }
diff --git a/src/lib/tsreader/MepoRTSPClient.cpp b/src/lib/tsreader/MepoRTSPClient.cpp
index 4971986..ccd6761 100644
--- a/src/lib/tsreader/MepoRTSPClient.cpp
+++ b/src/lib/tsreader/MepoRTSPClient.cpp
@@ -18,13 +18,14 @@
 
 #if defined LIVE555
 
-#include "p8-platform/util/timeutils.h"
 #include "MepoRTSPClient.h"
 #include "MemorySink.h"
 #include <kodi/General.h> //for kodi::Log
 #include "utils.h"
 #include "os-dependent.h"
 
+#include <thread>
+
 CRTSPClient::CRTSPClient()
 {
   kodi::Log(ADDON_LOG_DEBUG, "CRTSPClient::CRTSPClient()");
@@ -531,7 +532,7 @@ void CRTSPClient::FillBuffer(unsigned long byteCount)
 
   while ( IsRunning() && m_buffer->Size() < byteCount)
   {
-    usleep(5000);
+    std::this_thread::sleep_for(std::chrono::milliseconds(5));
     if (GetTickCount64() - tickCount > 3000)
       break;
   }
diff --git a/src/lib/tsreader/MultiFileReader.cpp b/src/lib/tsreader/MultiFileReader.cpp
index 3828e06..21fd7b2 100644
--- a/src/lib/tsreader/MultiFileReader.cpp
+++ b/src/lib/tsreader/MultiFileReader.cpp
@@ -39,10 +39,11 @@
 #include <string>
 #include "utils.h"
 #include <algorithm>
-#include "p8-platform/util/timeutils.h"
 #include "p8-platform/threads/threads.h"
 #include <inttypes.h>
 
+#include <thread>
+
 using namespace P8PLATFORM;
 
 //Maximum time in msec to wait for the buffer file to become available - Needed for DVB radio (this sometimes takes some time)
@@ -112,7 +113,7 @@ namespace MPTV
             retryCount++;
             kodi::Log(ADDON_LOG_DEBUG, "MultiFileReader: buffer file has zero length, closing, waiting 100 ms and re-opening. Attempt: %d.", retryCount);
             m_TSBufferFile.CloseFile();
-            usleep(100000);
+            std::this_thread::sleep_for(std::chrono::milliseconds(100));
             hResult = m_TSBufferFile.OpenFile();
             kodi::Log(ADDON_LOG_DEBUG, "MultiFileReader: buffer file opened return code %d.", hResult);
         }
@@ -124,7 +125,7 @@ namespace MPTV
 
             do
             {
-                usleep(100000);
+                std::this_thread::sleep_for(std::chrono::milliseconds(100));
                 if (timeout.TimeLeft() == 0)
                 {
                     kodi::Log(ADDON_LOG_ERROR, "MultiFileReader: timed out while waiting for buffer file to become available");
@@ -457,7 +458,7 @@ namespace MPTV
                 // try to clear local / remote SMB file cache. This should happen when we close the filehandle
                 m_TSBufferFile.CloseFile();
                 m_TSBufferFile.OpenFile();
-                usleep(5000);
+                std::this_thread::sleep_for(std::chrono::milliseconds(5));
             }
 
             if (Error)
diff --git a/src/lib/tsreader/TSReader.cpp b/src/lib/tsreader/TSReader.cpp
index 4a72f4f..40a4faf 100644
--- a/src/lib/tsreader/TSReader.cpp
+++ b/src/lib/tsreader/TSReader.cpp
@@ -33,7 +33,6 @@
 #include "MultiFileReader.h"
 #include "utils.h"
 #include "TSDebug.h"
-#include "p8-platform/util/timeutils.h"
 #include <kodi/tools/StringUtils.h>
 #ifdef LIVE555
 #include "MemoryReader.h"
@@ -42,6 +41,8 @@
 #endif
 #include "FileUtils.h"
 
+#include <thread>
+
 using namespace std;
 
 template<typename T> void SafeDelete(T*& p)
@@ -396,7 +397,7 @@ namespace MPTV
                 fileReader->OnChannelChange();
 
                 kodi::Log(ADDON_LOG_DEBUG, "%s:: move from %I64d to %I64d tsbufpos  %I64d", __FUNCTION__, pos_before, pos_after, timeShiftBufferPos);
-                usleep(100000);
+                std::this_thread::sleep_for(std::chrono::milliseconds(100));
 
                 // Set the stream start times to this new channel
                 time(&m_startTime);
diff --git a/src/pvrclient-mediaportal.cpp b/src/pvrclient-mediaportal.cpp
index 5479950..851b940 100644
--- a/src/pvrclient-mediaportal.cpp
+++ b/src/pvrclient-mediaportal.cpp
@@ -10,7 +10,6 @@
 #include <stdlib.h>
 #include <clocale>
 
-#include "p8-platform/util/timeutils.h"
 #include <kodi/tools/StringUtils.h>
 
 #include "timers.h"
@@ -29,6 +28,8 @@
 #include <kodi/General.h>
 #include <kodi/Filesystem.h>
 
+#include <thread>
+
 using namespace kodi::tools;
 using namespace std;
 using namespace MPTV;
@@ -389,7 +390,7 @@ void* cPVRClientMediaPortal::Process(void)
     if (keepWaiting)
     {
       // Wait for 1 minute before re-trying
-      usleep(60000000);
+      std::this_thread::sleep_for(std::chrono::milliseconds(60000));
     }
   }
   SetConnectionState(state);
@@ -1571,7 +1572,8 @@ PVR_ERROR cPVRClientMediaPortal::AddTimer(const kodi::addon::PVRTimer& timerinfo
   if (timerinfo.GetStartTime() <= 0)
   {
     // Refresh the recordings list to see the newly created recording
-    usleep(100000);
+    std::this_thread::sleep_for(std::chrono::milliseconds(100));
+
     kodi::addon::CInstancePVRClient::TriggerRecordingUpdate();
   }
 
@@ -1785,7 +1787,8 @@ bool cPVRClientMediaPortal::OpenLiveStream(const kodi::addon::PVRChannel& channe
       kodi::Log(ADDON_LOG_INFO, "Channel timeshift buffer: %s", timeshiftfields[2].c_str());
       if (channelinfo.GetIsRadio())
       {
-        usleep(100000); // 100 ms sleep to allow the buffer to fill
+        // 100 ms sleep to allow the buffer to fill
+        std::this_thread::sleep_for(std::chrono::milliseconds(100));
       }
     }
     else
@@ -1796,7 +1799,7 @@ bool cPVRClientMediaPortal::OpenLiveStream(const kodi::addon::PVRChannel& channe
     if (CSettings::Get().GetSleepOnRTSPurl() > 0)
     {
       kodi::Log(ADDON_LOG_INFO, "Sleeping %i ms before opening stream: %s", CSettings::Get().GetSleepOnRTSPurl(), timeshiftfields[0].c_str());
-      usleep(CSettings::Get().GetSleepOnRTSPurl() * 1000);
+      std::this_thread::sleep_for(std::chrono::milliseconds(CSettings::Get().GetSleepOnRTSPurl()));
     }
 
     // Check the returned stream URL. When the URL is an rtsp stream, we need
@@ -1878,7 +1881,7 @@ bool cPVRClientMediaPortal::OpenLiveStream(const kodi::addon::PVRChannel& channe
           CloseLiveStream();
           return false;
         }
-        usleep(400000);
+        std::this_thread::sleep_for(std::chrono::milliseconds(400));
       }
     }
 
@@ -1924,7 +1927,7 @@ int cPVRClientMediaPortal::ReadLiveStream(unsigned char *pBuffer, unsigned int i
 
     if (m_tsreader->Read(bufptr, read_wanted, &read_wanted) > 0)
     {
-      usleep(20000);
+      std::this_thread::sleep_for(std::chrono::milliseconds(20));
       read_timeouts++;
       return static_cast<int>(read_wanted);
     }
@@ -1948,7 +1951,7 @@ int cPVRClientMediaPortal::ReadLiveStream(unsigned char *pBuffer, unsigned int i
       }
       bufptr += read_wanted;
       read_timeouts++;
-      usleep(10000);
+      std::this_thread::sleep_for(std::chrono::milliseconds(10));
     }
   }
   read_timeouts = 0;
@@ -2175,7 +2178,7 @@ int cPVRClientMediaPortal::ReadRecordedStream(unsigned char *pBuffer, unsigned i
 
     if (m_tsreader->Read(bufptr, read_wanted, &read_wanted) > 0)
     {
-      usleep(20000);
+      std::this_thread::sleep_for(std::chrono::milliseconds(20));
       return static_cast<int>(read_wanted);
     }
     read_done += read_wanted;
@@ -2183,7 +2186,7 @@ int cPVRClientMediaPortal::ReadRecordedStream(unsigned char *pBuffer, unsigned i
     if ( read_done < static_cast<size_t>(iBufferSize) )
     {
       bufptr += read_wanted;
-      usleep(20000);
+      std::this_thread::sleep_for(std::chrono::milliseconds(20));
     }
   }
 
